﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;

namespace EAGL.Explore
{
    public class FeatureLayerExplorer
    {
       #region =========================== PRIVATE ===============================

        private IFeatureLayer layer;

        private int getFieldCount()
        {
            return layer.FeatureClass.Fields.FieldCount;
        }

        private int getVisibleFieldCount()
        {
            ILayerFields lf = layer as ILayerFields;
            int c = lf.FieldCount;
            int x = 0;
            for (int i = 0; i < c; i++)
            {
                if (lf.get_FieldInfo(i).Visible)
                {
                    x++;
                }

            }
            return x;
        }
        
        private int getInvisibleFieldCount()
        {
            ILayerFields lf = layer as ILayerFields;
            int c = lf.FieldCount;
            int x = 0;
            for (int i = 0; i < c; i++)
            {
                if (!lf.get_FieldInfo(i).Visible)
                {
                    x++;
                }

            }
            return x;
        }


        private List<IField> getFields()
        {
            List<IField> fields = new List<IField>();
            int c = layer.FeatureClass.Fields.FieldCount;
            for (int i = 0; i < c; i++)
            {
                fields.Add(layer.FeatureClass.Fields.get_Field(i));
            }
            return fields;
        }

        private List<IField> getVisibleFields()
        {
            List<IField> fields = new List<IField>();

            ILayerFields lf = layer as ILayerFields;
            int c = lf.FieldCount;
            for (int i = 0; i < c; i++)
            {
                if (lf.get_FieldInfo(i).Visible)
                {
                    fields.Add(layer.FeatureClass.Fields.get_Field(i));
                }

            }
            return fields;
        }

        private List<IField> getInvisibleFields()
        {
            List<IField> fields = new List<IField>();

            ILayerFields lf = layer as ILayerFields;
            int c = lf.FieldCount;
            for (int i = 0; i < c; i++)
            {
                if (!lf.get_FieldInfo(i).Visible)
                {
                    fields.Add(layer.FeatureClass.Fields.get_Field(i));
                }

            }
            return fields;
        }

        private string getFieldNames(bool includeInvisible)
        {
            StringBuilder sb = new StringBuilder();
            ILayerFields lf = layer as ILayerFields;
            int c = lf.FieldCount;
            bool v;
            for (int i = 0; i < c; i++)
            {
                v = (lf.get_FieldInfo(i).Visible);
                if (includeInvisible || v)
                {
                    sb.Append(i.ToString() + ": ");
                    sb.Append(layer.FeatureClass.Fields.get_Field(i).Name);
                    if (i < c - 1)
                        sb.Append(", ");
                }
            }
            return sb.ToString();
        }


       #endregion

       #region =========================== PUBLIC ================================
        
        public FeatureLayerExplorer(IFeatureLayer layer)
        {
            this.layer = layer;
        }

        public IFeatureLayer Layer
        {
            get { return this.layer; }
            set { this.layer = value; }
        }

        public int FieldCount
        {
            get { return getFieldCount(); }
        }

        public int VisibleFieldCount
        {
            get { return getVisibleFieldCount(); }
        }

        public int InvisibleFieldCount
        {
            get { return getInvisibleFieldCount(); }
        }

        public List<IField> Fields
        {
            get { return getFields(); }
        }

        public List<IField> VisibleFields
        {
            get { return getVisibleFields(); }
        }

        public List<IField> InvisibleFields
        {
            get { return getInvisibleFields(); }
        }

        
        



        
        
       #endregion

    }
}
